import {LOGOUT_PATH} from "@/constants";
import {message} from "antd";
import {DEFAULT_WS_TOPICS, useStompSubscription} from "kf-web-library";
import React from 'react';
import {history, useModel} from "umi";

const WebscoketGlobalHandle: React.FC<any> = () => {
  const {initialState, setInitialState} = useModel('@@initialState');
  const authToken = initialState?.currentAuthInfo?.token;

  useStompSubscription(DEFAULT_WS_TOPICS.BROADCAST, (msg) => {
    //广播通知的
  })

  useStompSubscription(DEFAULT_WS_TOPICS.USER_TOPIC, (msg) => {
    //点不点通知的
    const kickedToken = msg?.extras?.kickedToken;
    if (kickedToken === authToken) {
      message.error("你的账号已在其它地方登录", 5)
      history.push(LOGOUT_PATH);
    }
  }, ["USER_ACCOUNT_KICKED_OUT"])

  return null;
};

export default WebscoketGlobalHandle;
